class Project < ActiveRecord::Base
  
  has_many :requirements
  has_many :issues
  has_many :tags

  belongs_to :user

  state_machine :state, :initial => :need_to_release do

    after_transition any => :released, :do => :zero_priority
    after_transition any => :freeze, :do => :under_zero_priority

    event :working do
      transition :need_to_release => :released, :released => :freeze
    end

    event :rework do
      transition :freeze => :released
    end
    
  end

  def self.get_working_tags(p)
    p.tags.find_all_by_state("working", :order => 'should_release_at ASC')
  end

  def self.get_unfrozen_projects
    all(:conditions => ["state IN (?)", ["need_to_release", "released"]], :order => 'priority DESC, should_be_finish_time ASC')
  end

  def zero_priority
    self.priority = 0
    self.actually_finish_time = Time.now.to_date
    self.save
  end

  def under_zero_priority
    self.priority = -1
    self.actually_finish_time = Time.now.to_date
    self.save
  end

end
